<script setup>

import CardBox from "@/components/CardBox/index.vue";
import {useRouter} from "vue-router";
import {Download} from "@element-plus/icons-vue";
import {changeAcademicAchievementApi, getStuListApi} from "@/api/school.js";
import {getToken} from "@/utils/auth.js";
import useUserStore from "@/store/modules/user.js";
import {ElMessage, ElMessageBox} from "element-plus";

const {proxy} = getCurrentInstance();

const router = useRouter()
const state = reactive({
  teacherType: 0,
  permission: [],
  form: {},
  total: 0,
  tableData: [],
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    stuName: '',
    stuFaculties: '',
    stuPhone: '',
    stuPlace: '',
  },
  choiceList: null,
  uploadData: {
    stuId: '',
    type: '',
  },
  uploadUrl: '/school/leaningFileData/importData'
});

const showDia1 = ref(false)
const handleAdd = (type) => {
  // if (state.choiceList == null) {
  //   return proxy.$message.error('请选择学生')
  // }
  // 删除提示
  ElMessageBox.confirm(
      '上传表的第一列务必为学号',
      '提示',
      {
        confirmButtonText: '确认',
        type: 'warning',
      }
  )
      .then(() => {
        state.uploadData.type = type
        proxy.$refs['subBtn'].$el.click()
      })
}

const handleUploadSuccess = (response, file, fileList) => {
  console.log(response, file, fileList)
}

const handleEdit = (index) => {
  router.push('noticeEdit')
}

const handleJump = (type, id) => {
  const str = `?stuId=${id}&type=${type}`
  if (type === 1) {
    router.push(`/baseData/comprehensiveIn/sixTwoOneOne` + str)
  } else if (type === 3) {
    router.push(`/baseData/comprehensiveIn/scholarship` + str)
  } else if (type === 2) {
    router.push('/baseData/comprehensiveIn/ieAchievement' + str)
  } else if (type === 4) {
    router.push('/stuCphQuality/comprehensiveQuality/details' + str)
  } else if (type === 5) {


  }
}

const getDataList = async () => {
  const res = await getStuListApi(state.queryParams)
  if (res.code == 200) {
    state.tableData = res.rows
    state.total = res.total
  }
}

const resetSearch = () => {
  proxy.resetForm("queryRef");
  state.queryParams = {
    pageNum: 1,
    pageSize: 10,
  }
  getDataList()
}

const selectable = (row, index) => {
  return true;
}

// 选择事件处理函数
function handleSelectionChange(selection) {
  // 如果选中的行数大于1，取消其他行的选择，只保留最后选中的那一行
  if (selection.length > 1) {
    proxy.$refs['singleTable'].clearSelection();
    proxy.$refs['singleTable'].toggleRowSelection(selection[selection.length - 1], true);
  }
  // 更新选中的行数据
  if (selection[0]) {
    state.choiceList = selection[0];
    state.uploadData.stuId = selection[0].id
  } else {
    state.choiceList = null
    state.uploadData.stuId = ''
  }

}

const uploadUrl = (url) => {
  return import.meta.env.VITE_APP_BASE_API + url
}

const headers = ref({Authorization: "Bearer " + getToken()});

const uploadSuc = (data) => {
  if (data.code === 200) {
    proxy.$message.success('导入成功')
  } else {
    proxy.$message.error(data.msg)
  }
}
const uploadErr = (e) => {
  proxy.$message.error('导入失败')
}

// 改变优秀
const changeGood = async (e, stuId) => {
  const data = {
    id: stuId,
    isGood: e
  }
  const res = await changeAcademicAchievementApi(data)
  if (res.code === 200) {
    proxy.$message.success('修改成功')
    await getDataList()
  } else {
    proxy.$message.error(res.msg)
  }
}

const handleExport = () => {
  if (state.choiceList == null) {
    return proxy.$message.error('请选择学生')
  }

  const item = state.choiceList
  proxy.download("/school/student/exportResume", {
    id: item.id
  }, `${item.stuName}简历.docx`);
}
const userStore = useUserStore()
// 获取当前老师的权限信息
const getPermission = async () => {
  state.permission = userStore.roles
  if (state.permission.includes('teacher')) {
    state.teacherType = 0
  } else if (state.permission.includes('teacher2')) {
    state.teacherType = 1
  } else if (state.permission.includes('duizhang')) {
    state.teacherType = 3
  } else if (state.permission.includes('superManager')) {
    state.teacherType = 2
  }
}

onMounted(() => {
  getPermission()
  getDataList()
})
</script>

<template>
  <div class="outBox">
    <CardBox style="margin-bottom: 20px" title="筛选查询">
      <template #btn-cont>
        <el-button type="primary" @click="getDataList">查询</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </template>
      <template #content>
        <el-form :model="state.queryParams" label-width="auto" inline="true">
          <el-form-item label="姓名">
            <el-input v-model="state.queryParams.stuName"/>
          </el-form-item>
          <el-form-item label="学号">
            <el-input v-model="state.queryParams.stuNum"/>
          </el-form-item>
          <el-form-item label="班级">
            <el-input v-model="state.queryParams.stuClass"/>
          </el-form-item>
          <!--          <el-form-item label="院系">-->
          <!--            <el-input v-model="state.queryParams.stuFaculties"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="联系电话">-->
          <!--            <el-input v-model="state.queryParams.stuPhone"/>-->
          <!--          </el-form-item>-->
        </el-form>
      </template>
    </CardBox>
    <CardBox title="数据列表">
      <template #btn-cont>
        <el-upload
            ref="uploadRef"
            class="upload-demo"
            :action="uploadUrl(state.uploadUrl)"
            :auto-upload="true"
            :headers="headers"
            :data="state.uploadData"
            style="display: none;"
            :on-success="uploadSuc"
            :on-error="uploadErr"
        >
          <el-button type="primary" ref="subBtn"></el-button>
        </el-upload>
        <el-button type="primary" @click="handleAdd(1)" v-if="state.teacherType == 3 || state.teacherType == 2">
          6211导入
        </el-button>
        <el-button type="primary" @click="handleAdd(2)" v-if="state.teacherType == 3 || state.teacherType == 2">
          智育导入
        </el-button>
        <el-button type="primary" @click="handleAdd(3)" v-if="state.teacherType == 3 || state.teacherType == 2">
          奖学金导入
        </el-button>

        <el-select v-model="state.teacherType" placeholder="请选择"
                   style="width: 150px;margin-left: 15px">
          <el-option label="学业导师" :value="0" v-if="state.permission.includes('teacher')"/>
          <el-option label="专业教师" :value="1" v-if="state.permission.includes('teacher2')"/>
          <el-option label="管理员" :value="2" v-if="state.permission.includes('superManager')"/>
          <el-option label="中队长" :value="3" v-if="state.permission.includes('duizhang')"/>
        </el-select>
      </template>
      <template #content>
        <el-table :data="state.tableData" style="width: 100%" ref="singleTable"
                  @selection-change="handleSelectionChange">
          <el-table-column type="selection" :selectable="selectable" width="55"/>
          <el-table-column prop="stuName" label="姓名" align="center"/>
          <el-table-column prop="sex" label="性别" align="center"/>
          <el-table-column prop="stuNum" label="学号" align="center"/>
          <el-table-column prop="yearEnrollment" label="年级" align="center"/>
          <el-table-column prop="stuClass" label="班级" align="center"/>
          <el-table-column prop="e" label="6211档案" align="center">
            <template #default="scope">
              <el-button type="primary" size="small" link @click="handleJump(1,scope.row.id)">查看详情</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="e" label="奖学金展示" align="center">
            <template #default="scope">
              <el-button type="primary" size="small" link @click="handleJump(3,scope.row.id)">查看详情</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="e" label="智育成绩" align="center">
            <template #default="scope">
              <el-button type="primary" size="small" link @click="handleJump(2,scope.row.id)">查看详情</el-button>
            </template>
          </el-table-column>
          <!--          <el-table-column prop="e" label="综合评定" align="center">-->
          <!--            <template #default="scope">-->
          <!--              <el-button type="primary" size="small" link @click="handleJump(4,scope.row.id)">查看详情</el-button>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
        </el-table>

        <div class="pageBox">
          <pagination
              v-show="state.total > 0"
              :total="state.total"
              v-model:page="state.queryParams.pageNum"
              v-model:limit="state.queryParams.pageSize"
              @pagination="getDataList()"/>
        </div>
      </template>
    </CardBox>
  </div>
</template>

<style scoped lang="scss">

</style>
